package com.leoandroid.tool.toolsbox

import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.leoandroid.tool.toolsbox.adapter.ZodiacDetailAdapter
import com.leoandroid.tool.toolsbox.bean.ZodiacBean
import com.leoandroid.tool.toolsbox.largeread.databinding.ActivityChineseZodiacDetailsBinding
import com.leoandroid.tool.toolsbox.net.NetCallBack
import com.leoandroid.tool.toolsbox.net.NetRequest
import org.json.JSONObject
//生肖查询
class ChineseZodiacDetailsActivity:AppCompatActivity() {

    var bindingF: ActivityChineseZodiacDetailsBinding?=null
    val binding get() = bindingF!!
    var value:String=""
    var keyName:String=""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bindingF = ActivityChineseZodiacDetailsBinding.inflate(layoutInflater)
        setContentView(binding.root)
        if(intent != null){
            value = intent.getStringExtra("KEY").toString()
        }
        binding.includeTitle.backIv.setOnClickListener {
            finish()
        }
        if(!TextUtils.isEmpty(value) && value.length>=2){
            keyName = value[1].toString()
        }
        binding.includeTitle.titleTv.text = value
        val url = "http://apis.juhe.cn/fapig/zodiac/query?key=8263828f8a4338436570c972ad9e9499&keyword=${keyName}"
        NetRequest.asyncGet(url,object:NetCallBack{
            override fun onFailure(code: Int, e: Throwable?) {

            }

            override fun onSuccess(json: JSONObject?) {
                Log.d("xxxxxxx","json>>"+json.toString())
                val result = json?.getJSONObject("result")
                runOnUiThread {
                    val zodiacBean = Gson().fromJson(result.toString(), ZodiacBean::class.java)
                    binding.nameTv.text = "生肖名称:"+zodiacBean.name
                    binding.yearTv.text = "生肖年份:"+zodiacBean.years
                    binding.fwTv.text = "吉祥方位:"+zodiacBean.fw
                    binding.scTv.text = "吉忌颜色:"+zodiacBean.sc
                    binding.szTv.text = "吉凶数字:"+zodiacBean.sz
                    binding.xyhTv.text = "幸运花："+zodiacBean.xyh
                    binding.ysTv.text = "总体运势:"+zodiacBean.ys
                    binding.syTv.text = "事业:"+zodiacBean.sy
                    binding.aqTv.text = "爱情:"+zodiacBean.aq
                    binding.xgTv.text = "性格:"+zodiacBean.xg
                    binding.ydTv.text = "优点:"+zodiacBean.yd
                    binding.qdTv.text = "缺点:"+zodiacBean.qd
                    binding.bxTv.text = "基本表现:"+zodiacBean.bx
                    binding.currentAgeTv.text = "出生年份今年年龄:"
                    val subAdapter = ZodiacDetailAdapter(this@ChineseZodiacDetailsActivity,zodiacBean.currentAge)
                    val layoutManager = LinearLayoutManager(this@ChineseZodiacDetailsActivity)
                    binding.recycleview.adapter = subAdapter
                    binding.recycleview.layoutManager = layoutManager

                }

            }
        })
    }
}